package com.dmgis.qxfw.dataservice.businessservice.yyhx.service.Impl;

import com.dmgis.qxfw.dataservice.businessservice.api.domain.DbConfig;
import com.dmgis.qxfw.dataservice.businessservice.api.domain.RequestDataBySelect;
import com.dmgis.qxfw.dataservice.businessservice.api.entity.zy.base_product;
import com.dmgis.qxfw.dataservice.businessservice.api.entity.zy.ccshx_imp_dot;
import com.dmgis.qxfw.dataservice.businessservice.api.entity.zy.cshx_zsyb;
import com.dmgis.qxfw.dataservice.businessservice.common.util.DateTime;
import com.dmgis.qxfw.dataservice.businessservice.common.util.JsonUtil;
import com.dmgis.qxfw.dataservice.businessservice.common.util.StringUtil;
import com.dmgis.qxfw.dataservice.businessservice.controller.ProcessRequestController;
import com.dmgis.qxfw.dataservice.businessservice.yyhx.service.IFirefcstService;
import com.fasterxml.jackson.core.type.TypeReference;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service("FireService")
public class FirefcstServiceImpl implements IFirefcstService {
	ProcessRequestController process = new ProcessRequestController();

	@Override
	public cshx_zsyb initcshxPage() {
		// TODO 火险指数初始化
		cshx_zsyb newDate = new cshx_zsyb();
		Map<String, String> selectParam = new HashMap<String, String>();
		RequestDataBySelect selectRequest = new RequestDataBySelect();
		String[] columns = new String[] { "to_char(forcastdate, 'yyyy-MM-dd HH24:MI:SS') as forcastdate"};
		selectRequest.setColumns(columns);
		selectRequest.setSort(new String[] { "forcastdate desc"});
		selectRequest.setLimit(1);
		try {
			selectParam.put("method", "cshx_zsyb");
			selectParam.put("sqlType", "select");
			selectParam.put("advCode", DbConfig.advCode);
			selectParam.put("param", JsonUtil.object2Json(selectRequest));
			String message = process.processRequest(selectParam);
			newDate = JsonUtil.jsonStr2TypeReference(message, new TypeReference<List<cshx_zsyb>>() {
			}).get(0);
		} catch (Exception e) {
			// TODO: handle exception
			newDate.setForcastdate(DateTime.now().toDate());
		}
		return newDate;
	}

	@Override
	public List<cshx_zsyb> getFireIndex(String date) {
		// TODO 获取指数查看
		List<cshx_zsyb> listData = new ArrayList<cshx_zsyb>();
		Map<String, String> selectParam = new HashMap<String, String>();
		RequestDataBySelect selectRequest = new RequestDataBySelect();
		List<String> columns = new ArrayList<String>();
		columns.add("stationid");
		columns.add("forcastdate");
		columns.add("fireindex");
		columns.add("firelevel");
		columns.add("stationname");
		columns.add("lon");
		columns.add("lat");
		selectRequest.setColumns(columns.toArray(new String[columns.size()]));
		// 拼接where字段
		StringBuilder strWhere = new StringBuilder();
		if (!StringUtil.IsNullOrEmpty(date)) {
			strWhere.append(String.format(" and forcastdate='%s'", date));
		}
		if (!StringUtil.IsNullOrEmpty(strWhere.toString())) {
			strWhere = strWhere.replace(0, 4, "");
		}
		selectRequest.setParam(strWhere.toString());
		try {
			selectParam.put("method", "cshx_zsyb");
			selectParam.put("sqlType", "select");
			selectParam.put("advCode", DbConfig.advCode);
			selectParam.put("param", JsonUtil.object2Json(selectRequest));
			String message = process.processRequest(selectParam);
			listData = JsonUtil.jsonStr2TypeReference(message, new TypeReference<List<cshx_zsyb>>() {
			});
		} catch (Exception e) {
			// TODO: handle exception
		}
		return listData;
	}

	@Override
	public List<ccshx_imp_dot> getImpdot(String stationname) {
		// TODO 重要目标
		List<ccshx_imp_dot> listData = new ArrayList<ccshx_imp_dot>();
		Map<String, String> selectParam = new HashMap<String, String>();
		RequestDataBySelect selectRequest = new RequestDataBySelect();
		// 拼接where字段
		StringBuilder strWhere = new StringBuilder();
		if (!StringUtil.IsNullOrEmpty(stationname)) {
			strWhere.append(String.format(" and joinstation='%s'", stationname));
		}
		if (!StringUtil.IsNullOrEmpty(strWhere.toString())) {
			strWhere = strWhere.replace(0, 4, "");
		}
		selectRequest.setParam(strWhere.toString());
		try {
			selectParam.put("method", "ccshx_imp_dot");
			selectParam.put("sqlType", "select");
			selectParam.put("advCode", DbConfig.advCode);
			selectParam.put("param", JsonUtil.object2Json(selectRequest));
			String message = process.processRequest(selectParam);
			listData = JsonUtil.jsonStr2TypeReference(message, new TypeReference<List<ccshx_imp_dot>>() {
			});
		} catch (Exception e) {
			// TODO: handle exception
		}
		return listData;
	}

	@Override
	public List<base_product> showDoc(String cpName) {
		// TODO 火险文档
		List<base_product> listData = new ArrayList<base_product>();
		Map<String, String> selectParam = new HashMap<String, String>();
		RequestDataBySelect selectRequest = new RequestDataBySelect();
		// 拼接where字段
		StringBuilder strWhere = new StringBuilder();
		if (!StringUtil.IsNullOrEmpty(cpName)) {
			strWhere.append(String.format(" and productname='%s'", cpName));
		}
		strWhere.append(String.format(" and producttype='%s'", "城市火险预报"));
		if (!StringUtil.IsNullOrEmpty(strWhere.toString())) {
			strWhere = strWhere.replace(0, 4, "");
		}
		selectRequest.setParam(strWhere.toString());
		try {
			selectParam.put("method", "base_product");
			selectParam.put("sqlType", "select");
			selectParam.put("advCode", DbConfig.advCode);
			selectParam.put("param", JsonUtil.object2Json(selectRequest));
			String message = process.processRequest(selectParam);
			listData = JsonUtil.jsonStr2TypeReference(message, new TypeReference<List<base_product>>() {
			});
		} catch (Exception e) {
			// TODO: handle exception
		}
		return listData;
	}

}